package com.zhiyou.app.shop.manager.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.zhiyou.app.shop.manager.dao.IGoodsDao;
import com.zhiyou.app.shop.manager.dao.pojo.Goods;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsImg;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsVisitLog;

@Repository
public class GoodsDaoImpl extends BaseHibernateDAO<Goods, String> implements IGoodsDao {
	
	public GoodsDaoImpl(){
		super(Goods.class);
	}
	
	public List<GoodsImg> findGoodsImgByGoodsId(String goodsId, Integer length){
		Session session = this.getSession();
		String hql = "from "+GoodsImg.class.getName()+" where goods.id=:goodsId order by orderNumber";
		Query query = session.createQuery(hql);
		query.setParameter("goodsId", goodsId);
		if(null != length){
			query.setFirstResult(0);
			query.setMaxResults(length);
		}
		return query.list();
	}

	public void setSaleState(String ids, boolean onSale) {
		String hql = "update "+Goods.class.getName()+" set onSale=:onSale" +
				" where id in ("+ids+")";
		
		Query query = getSession().createQuery(hql);
		query.setBoolean("onSale", onSale);
		query.executeUpdate();
	}

	public void saveGoodsVisitLog(GoodsVisitLog goodsVisitLog) {
		this.getSession().save(goodsVisitLog);
	}
	
}
